package com.controller;


import com.github.pagehelper.PageInfo;
import com.pojo.Sitepoint;
import com.pojo.Siteroute;
import com.service.SiteService;
import com.vo.Result;
import com.vo.SiterouteVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/Site")
public class SiteRouteController {
    @Resource
    private SiteService siteService;

//巡查路线
    @RequestMapping("/Siteroute/index")
    public String toSiteroute(){
        return "/Site/SiteRoute";
    }

    @RequestMapping("/Siteroute/show")
    @ResponseBody
    public Result select(@RequestBody SiterouteVo siterouteVo){
        PageInfo<Siteroute> siteroutePageInfo = siteService.selectAllSiteRoute(siterouteVo);
        return Result.success(siteroutePageInfo);
    }

    @RequestMapping("/Siteroute/show2")
    @ResponseBody
    public Result select(){
        Integer id=1;
        List<Siteroute> siteroutes = siteService.selectAllSiteRoute2(id);
        return Result.success(siteroutes);
    }

    @RequestMapping("/Siteroute/delete")
    @ResponseBody
    public Result delete(Integer id){
        try {
            Integer selectroutestatebyid = siteService.selectroutestatebyid(id);
            if (selectroutestatebyid==1){

                //删除路线同时修改巡检点的是否包含状态
                Sitepoint sitepoint=new Sitepoint();

                List<Integer> selectpointids = siteService.selectpointidsaaabbb(id);
                for (Integer selectpointid : selectpointids) {

                    sitepoint.setRouteid(selectpointid);
                    sitepoint.setShifou(2);
                    siteService.updateSitepoint(sitepoint);

                }

                siteService.deleteSiteRoutebyid(id);
                return Result.success("删除成功");
            }else {
                return Result.success("该路线正在使用,无法删除");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("删除失败");
        }
    }

    @RequestMapping("/Siteroute/add")
    @ResponseBody
    public Result add(Siteroute siteroute){
        try {
            siteroute.setStartend(1);

            siteService.addSiteRoute(siteroute);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }

    @RequestMapping("/Siteroute/update")
    @ResponseBody
    public Result update(Siteroute siteroute){
        try {
            siteService.updateSiteRoute(siteroute);
            return Result.success("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }
}
